package com.zhida.smartcontrol.pagectrl.projectdetail.search;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jay.tools.ui.TopBarView;
import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.mvp.MVPBaseFragment;
import com.zhida.smartcontrol.pagectrl.projectdetail.GroupBean;
import com.zhida.smartcontrol.pagectrl.projectdetail.search.searchdetails.SearchDetailsFragment;
import com.zhida.smartcontrol.pagehome.ProjectResultBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class ProjectSearchFragment extends MVPBaseFragment<ProjectSearchContract.View, ProjectSearchPresenter> implements ProjectSearchContract.View {
    @BindView(R.id.top)
    TopBarView top;
    @BindView(R.id.Spinner01)
    Spinner Spinner01;
    @BindView(R.id.edit_search)
    EditText editSearch;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    private String mId;
    private String groupId;
    private ArrayAdapter<String> adapters;
    private ArrayList<String> data = new ArrayList<>();
    private List<GroupBean> datas;
    private ProjectSearchAdapter adapter;

    public static ProjectSearchFragment newInstance(String projectId, String projectName) {
        Bundle args = new Bundle();
        args.putString("id", projectId);
        args.putString("pName", projectName);
        ProjectSearchFragment fragment = new ProjectSearchFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected Object getLayout() {
        return R.layout.fragment_search_project;
    }

    @Override
    protected void initLazy(Bundle savedInstanceState) {
        mId = getArguments().getString("id");
        top.setTitle(getArguments().getString("pName"));
        top.setOnTopBarClickListener(new TopBarView.OnTopBarClick() {
            @Override
            public void leftClick() {
                pop();
            }

            @Override
            public void rightClick() {
            }
        });
        //将可选内容与ArrayAdapter连接起来
        adapters = new ArrayAdapter(requireContext(), android.R.layout.simple_spinner_item, data);

        //设置下拉列表的风格
        adapters.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        //将adapter 添加到spinner中
        Spinner01.setAdapter(adapters);
        //添加事件Spinner事件监听
        Spinner01.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                groupId = position == 0 ? "" : datas.get(position-1).getId();
                mPresenter.requestWarning("", mId, groupId, editSearch.getText().toString(), true);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //设置默认值
        Spinner01.setVisibility(View.VISIBLE);
        mPresenter.requestGroup(mId);
        tvSearch.setOnClickListener(v -> mPresenter.requestWarning("", mId, groupId, editSearch.getText().toString(), true));
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        initRecycler(recyclerView, 1, R.color.c_EEEEEE);
        adapter=new ProjectSearchAdapter(null);
        adapter.bindToRecyclerView(recyclerView);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.requestWarning("", mId, groupId, editSearch.getText().toString(), true);
                refresh.setRefreshing(false);
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapters, View view, int position) {
                start(SearchDetailsFragment.newInstance(adapter.getData().get(position).getId(),getArguments().getString("pName"),adapter.getData().get(position).getSwitchType()));
            }
        });
    }

    @Override
    public void showGroup(List<GroupBean> groupList) {
        datas = groupList;
        data.add("全项目");
        for (int i = 0; i < groupList.size(); i++) {
            data.add(groupList.get(i).getName());
        }
        adapters.notifyDataSetChanged();
    }

    @Override
    public void showData(List<ProjectResultBean> warningBean) {
        adapter.setNewData(warningBean);
    }
}
